<?php
namespace  zhtfrontend\components\services\goods;

use common\components\helpers\Fun;
use zhtfrontend\models\datas\ArtTeaching;
use zhtfrontend\models\datas\Goods;
use zhtfrontend\models\datas\GoodsPrice;
use yii\base\Action;
use yii\web\BadRequestHttpException;
use yii\web\NotFoundHttpException;
use yii\widgets\ActiveForm;
use yii\web\Response;

class UpdateSetAction extends Action{


    public function run(){
//        $this->controller->layout=false;
        $id = \Yii::$app->request->get('id');
        if ($id == null) {
            throw new BadRequestHttpException(\Yii::t('frontend', '错误请求'));
        }
        $model = GoodsPrice::findOne($id);
        if ($model ==null) {
            throw new NotFoundHttpException(\Yii::t('frontend', '信息不存在'));
        }
        $model->activity_start_time = $model->activity_start_time > 0?date('Y-m-d H:i',$model->activity_start_time):'';
        $model->activity_end_time = $model->activity_end_time>0?date('Y-m-d H:i',$model->activity_end_time):'';
        $modelgoods = Goods::findOne($model->goods_id);
        if ($modelgoods ==null) {
            throw new NotFoundHttpException(\Yii::t('frontend', '信息不存在'));
        }
//        $art = null;
//        if($modelgoods->is_group == 1){
//            $art = ArtTeaching::find()->where(['status'=>1,'type'=>$modelgoods->category_id])->one();
//        }
        //ajax验证
        if (\Yii::$app->request->isAjax && \Yii::$app->request->post('ajax') == 'w0' && $model->load(\Yii::$app->request->post())) {
            \Yii::$app->response->format = Response::FORMAT_JSON;
            return ActiveForm::validate($model);
        }

        if ($model->load(\Yii::$app->request->post()) && $model->validate()) {
            $model->activity_start_time = isset($model->activity_start_time)?strtotime($model->activity_start_time):0;
            $model->activity_end_time = isset($model->activity_end_time)?strtotime($model->activity_end_time):0;
            if($model->save(false)) {
                $url = Fun::url([$this->controller->id . '/setlist', 'id' => $model->goods_id]);
                if ($this->controller->id == 'goodsskill') {
                    $url = Fun::url([$this->controller->id . '/setlist', 'id' => $model->goods_id]);
                }
//                return $this->controller->redirectMessage('修改成功',$url);
                return $this->controller->redirectMessage(\Yii::t('zhtfrontend', '修改成功'), $url);
            }

        }
        return $this->controller->render('@zhtfrontend/components/services/goods/set.php',['model'=>$model,'data'=>$modelgoods]);
    }

}